
$(function() {
       //callbacl_url获取
        window.localStorage.setItem("callback_url",new configure().getUrlParam("callback_url"));
        var smkVersion=vsersionGet("smkVersion","3.10.0");
        if(smkVersion.result){//app中
            if(!smkVersion.isBig){//低版本<3.10.0
                $(".login-display").show();
            }
        }else{
            $(".login-display").show();
        }
        $("#showTooltips").click(function() {
            var user_id = $('#tel').val();//用户手机号
            var password = $('#pwd').val();//用户密码
            if (user_id =="") {$.toast('请输入手机号', 'forbidden'); return;}
            if(!/1[3|4|5|7|8]\d{9}/.test(user_id)){ $.toast('请输入正确的手机号', 'forbidden');   return;}
            if(password=="")  { $.toast('请输入登录密码', 'forbidden');  return;}
            var params={};
            params.user_id=user_id;
            params.password=password;
            params.user_system=new configure().getUrlParam("user_system");
            params.open_id=new configure().getUrlParam("open_id");
            new configure().connect(new configure().url_smk+"javaapi/smkuser-service/users/doLogin","POST",params,function (data,status,txtErr) {
                if(status=="success"){
                    if(data.code==0000){
                        //登录成功写入
                        $.toast('登录成功', 'ok',function () {
                            new configure().cookieConfig(data.data.wap_token,user_id);
                        });
                    }else{
                        $.toptip(data.msg);
                    }
                }else{
                    $.toast(txtErr, 'forbidden');
                }
            })
        });
        //忘记密码
        $("#forget_pwd").on('click',function () {
            var user_id = $('#tel').val();//用户手机号
            window.location.href="./forget.html?user_id="+user_id+"&user_system="+new configure().getUrlParam("user_system")+"&open_id="+new configure().getUrlParam("open_id")+"&callback_url="+new configure().getUrlParam("callback_url");
        });
        //注册
        $("#register").on('click',function () {
            window.location.href = "./register.html?user_system=" + new configure().getUrlParam("user_system")+"&open_id="+new configure().getUrlParam("open_id");;
        })
        //手机号输入字数限定
      $("#tel").bind('input propertychange',function () {
          if($(this).val().length>11){
              $(this).val($(this).val().substring(0,11));
          }
      })

});


//接受app回调token
function sendInfoToWap(token) {
	//$("#tel").val(navigator.userAgent.indexOf("smkVersion"));
     new configure().connect(new configure().url_smk+"javaapi/smkuser-service/users/app_token/"+token,"GET",{},function (data,status) {
         if(status=="success"){
             if(data.code==0000){
                //操作成功
                 var wap_token=data.data.wap_token;
                 var mobile=data.data.mobile;
                 localStorage.setItem("app_wap_token",token)
                 saveWapToken(wap_token,mobile);
            }else{
                 // window.location.replace( window.location);
             }
         }else{
                // window.location.replace( window.location);
         }
     })
}
//获得指定userAgent版本
function versionGet(strVersion,version) {
    var _versionStr=strVersion;
    var _userAgent=navigator.userAgent;

    if(_userAgent.indexOf(_versionStr)>-1){
        var reg=/smkVersion[\/|\s]([\d\.]*)/g;
        var result=reg.exec(_userAgent);
        console.log(result)
        //$("title").text(result.length);
        if(result&&result.length==2){//存在 strVersion
            var _smkVersion=result[1];
            _smkVersion=_smkVersion.split(".");
            _smkVersion=_smkVersion.map(function(a,b){console.log(a)
                return parseInt(a);
            })
            var _version=version.split(".");
            _version=_version.map(function(a,b){console.log(a)
                return parseInt(a);
            })
            var _result={result:true,isBig:true};
            for(var i=0;i<_smkVersion.length;i++){
                if(_smkVersion[i]>_version[i]){
                    _result= {result:true,isBig:true};//高版本
                    break;
                }else if(_smkVersion[i]<_version[i]){
                    _result= {result:true,isBig:false};//低版本
                    break;
                }
            }
            return _result;

        }else{ //不存在 strVersion
            return {result:false};
        }
    }else{ //不存在 strVersion
        return {result:false};
    }
}


function saveWapToken(wap_token,mobile) {
    var callback_url = window.localStorage.getItem("callback_url");
    var callback_url_new=decodeURIComponent(callback_url);
    var strRegex ='(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]';
    var re=new RegExp(strRegex);
    if(callback_url_new!="")
    {
        var host=callback_url_new.split("/")[2];
        var hostItem= host.split(".");
        var hostCount= hostItem.length;
        var url_path=hostItem[hostCount-2]+"."+hostItem[hostCount-1];
        // alert(url_path)
        if(url_path.indexOf(":")>-1){
            url_path=url_path.substring(0,url_path.indexOf(":"))
        }
        $.cookie('mobile', mobile,{ path: '/',domain:url_path,expires:7});
        $.cookie('wap_token', wap_token,{ path: '/',domain:url_path,expires:7});
        //$.cookie('o2osign', wap_token,{ path: '/',domain:url_path,expires:7});
        window.location.replace(callback_url_new);
        // }

    }else{
        alert("callback_url不能为空");
    }
}

